package game;

import java.util.*;

public class Game {
	private int id;
	private Sequence s1;
	private Sequence s2;
	private int player1;
	private int player2;
	private List<int[]> nTry1;
	private List<int[]> nTry2;
	private boolean endGame;
	
	
	public Game (int id, int player){
		this.id = id;
		this.player1 = player;
		endGame = false;
		nTry1 = new ArrayList<int[]>();
		nTry2 = new ArrayList<int[]>();
		s1 = null;
		s2 = null;
	}
	
	public int getId(){
		return id;
	}
	
	public void setPlayer2(int player){
		this.player2 = player;
	}
	
	public void setSequence1(Sequence s1){
		this.s1 = s1;
	}
	
	public void setSequence2(Sequence s2){
		this.s2 = s2;
	}
	
	public void play(char[] try1, char[] try2){
		int[] temp1 = s1.trySequence(try1);
		int[] temp2 = s2.trySequence(try2);
		nTry1.add(temp1);
		nTry2.add(temp2);
		if(checkGreen(temp1) || checkGreen(temp2)){
			endGame = true;
			
		}
	}
	
	public void play(char[] try1, int player){
		int[] temp;
		if(player == 1){
			temp = s2.trySequence(try1);
			nTry1.add(temp);
		}
		else  {
			temp = s1.trySequence(try1);
			nTry2.add(temp);
		}
		
		if(checkGreen(temp)){
			endGame = true;
		}
	}
	
	public boolean checkTurn(int player){
		if(player == 1 && nTry1.size() <= nTry2.size())return true;
		else if(player == 2 && nTry2.size() <= nTry1.size())return true;
		else return false;
	}
	
	private boolean checkGreen(int[] tentative){
		boolean c = true;
		for(int i = 0; i < tentative.length; i++){
			if(tentative[i] != 2) c = false; 
		}
		return c;
	}
	
	public boolean isGameOver(){
		return endGame;
	}
	
	public int[] getLastResult1(){
		return nTry1.get(nTry1.size() - 1);
	}
	
	public int[] getLastResult2(){
		return nTry2.get(nTry2.size() - 1);
	}
	
	public boolean isSequence1(){
		if(s1 == null) return false;
		else return true;
	}
	
	public boolean isSequence2(){
		if(s2 == null) return false;
		else return true;
	}
	
}
